<%@ page import="org.whp.mealplanner.server.PMF"%>
<%@ page import="org.whp.mealplanner.server.Ingredient"%>
<%@ page import="javax.jdo.PersistenceManager"%>
<%@ page import="java.util.List"%>
<%@ page import="javax.jdo.Extent"%>
<%@ page import="javax.jdo.Query"%>

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link rel="stylesheet" href="/style/addIngredient.css" type="text/css" />
<title>Add new ingredient</title>
<script
	src="http://www.google.com/jsapi?key=ABQIAAAANc4q3bIwmteElIO5__I_sBTwM0brOpm-All5BF6PoaKBxRWWERRlc8UGddZ4cFdAQssm2RJI7sQFAA"></script>
<script>
<!--	google.load("jquery", "1.3.2");-->
	
</script>
</head>
<body>
	<%
		int startIdx, endIdx;
		String startIdxParam = request.getParameter("startIdx");
		String endIdxParam = request.getParameter("endIdx");

		if (startIdxParam != null && endIdxParam != null) {
			startIdx = Integer.parseInt(startIdxParam);
			endIdx = Integer.parseInt(endIdxParam);
			//out.println("Parameters parsed successfully");
		} else {
			startIdx = 0;
			endIdx = 5;
		}
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Extent<Ingredient> extent = pm.getExtent(Ingredient.class, false);

		String query = "select from " + Ingredient.class.getName()
				+ " order by name asc";
		Query query2 = extent.getPersistenceManager().newQuery(query);
		query2.setRange(startIdx, endIdx);
		//query2.setRange(0,5);
		List<Ingredient> extentsIngredients = (List<Ingredient>) query2
				.execute();
		//String query = "select from " + Ingredient.class.getName();
		//List<Ingredient> ingredients = (List<Ingredient>) pm.newQuery(query).execute();
		if (!extentsIngredients.iterator().hasNext()) {
	%>

	<p>No ingredients yet.</p>
	<%
		} else {
	%>
	<form action="/removeIngredients" method="post" id="removeForm">
		<table>
			<%
				for (Ingredient i : extentsIngredients) { /*for (Ingredient i : ingredients) {*/
			%><tr>
				<td><%=i.getName()%></td>
				<td><input type="checkbox" name="<%=i.getEncodedKey()%>"
					value="remove" />
				</td>
			</tr>
			<%
				}
				}
				extent.closeAll();
				pm.close();
			%>
		</table>
		<a
			href="addIngredient.jsp?startIdx=<%=startIdx + 5%>&endIdx=<%=endIdx + 5%>">Next
			page</a> <input type="submit" name="removeIngredients"
			value="Remove selected" />
	</form>
	<div id="formWrapper">
		<p>Here you can add new ingredient. If one already is present it
			will shown to you and not save into database.</p>
		<table>
			<tbody>
				<form action="/addIngredient" method="post" id="addForm">
					<tr>
						<td>Name of ingredient:</td>
						<td><input type="text" name="name" />
						</td>
					</tr>
					<tr>
						<td>Units:</td>
						<td><input type="text" name="units" />
						</td>
					</tr>
					<tr>
						<td>&nbsp;</td>
						<td><input type="submit" name="addIngredient" value="Add" />
						</td>
					</tr>
			</tbody>
			</form>
		</table>



	</div>
</body>
</html>